<template>
  <section class="todoapp">
    <TodoHeader @getli="getliFn"></TodoHeader>
    <TodoMain :arr="lists" @move="moveFn"></TodoMain>
    <TodoFooter :arr="list" @clear="clearFn" @sum="sumFn"></TodoFooter>
  </section>
</template>

<script>
import TodoHeader from "./components/TodoHeader.vue";
import TodoMain from "./components/TodoMain.vue";
import TodoFooter from "./components/TodoFooter.vue";
// 样式引入 + 导入组件并注册使用
import "./styles/base.css";
import "./styles/index.css";
// 样式引入 + 导入组件并注册使用
export default {
  data() {
    return {
      one: "all",
      list: JSON.parse(localStorage.getItem("todos")) || [
        {
          id: 100,
          name: "吃饭",
          isDone: true,
        },
        {
          id: 201,
          name: "睡觉",
          isDone: false,
        },
        {
          id: 103,
          name: "打豆豆",
          isDone: true,
        },
      ],
    };
  },
  components: {
    TodoHeader,
    TodoMain,
    TodoFooter,
  },
  methods: {
    //删
    moveFn(id) {
      this.list = this.list.filter((item) => item.id !== id);
    },
    // 增加
    getliFn(val) {
      this.list.unshift({
        id: +new Date(),
        name: val,
        isDone: false,
      });
    },
    clearFn() {
      this.list = this.list.filter((item) => item.isDone === false);
    },

    sumFn(val) {
      this.one = val;
    },
  },
  computed: {
    lists() {
      if (this.one === "all") {
        return this.list;
      } else if (this.one === "mover") {
        return this.list.filter((item) => item.isDone === false);
      } else if (this.one === "over") {
        return this.list.filter((item) => item.isDone);
      }
    },
  },
  watch: {
    list: {
      deep: true,
      handler() {
        localStorage.setItem("todos", JSON.stringify(this.list));
      },
    },
  },
};
</script>
